<?php

function parseScores($textDump) {
	$uploadedSomething = false;
	$players = getPlayerReplacementNames();
	$round = getCurrentRound();
	$reqText = "starting" . substr($textDump, strpos($textDump, "playerCellShirt"));
	$lines = preg_split("%\n%", $reqText);
	$count = count($lines);
	$team = "";
	$playerId = null;
	$countPlayers = 0;
	for ($i = 0; $i < $count; $i++) {
		if (strpos($lines[$i], "playerCellShirt") != "") {
			if ($countPlayers > 15 && $team != "") {
				$uploadedSomething = true;
				updateZeroScoringPlayers($round, $team);
			}
			$countPlayers = 0;
			$start = strpos($lines[$i], "playerCellShirt") + 16;
			$teamVal = substr($lines[$i], $start, strpos($lines[$i], "\"", $start) - $start);
			$team = getAFLTeamMapKey($teamVal);
			$teamName = getAFLTeam($team);
		}
			
			//echo "(" . $teamVal . ", " . $team . ", " . $teamName . ")";
		if (strpos($lines[$i], "player-name") != "") {
			$start = strpos($lines[$i], ">", strpos($lines[$i], "player-name")) + 1;
			$names = preg_split("%, %", substr($lines[$i], $start, strpos($lines[$i], "<", $start) - $start));
			
			//echo "(" . $names[1] . "," . $names[0] . ")<br>";
			
		    $count2 = count($players);
		    for ($j = 0; $j < $count2; $j++) {
		    	if ($players[$j]->firstName == $names[1] && $players[$j]->surname == $names[0] && $players[$j]->team == $teamName) {
		    		$names[1] = $players[$j]->requiredFirstName;
		    		$names[0] = $players[$j]->requiredSurname;
		    		
		    		break;
		    	}
		    }
		    
		    $playerId = getPlayerID($names[1], $names[0], $team);
	        //echo "[" . $names[1] . "," . $names[0] . "," . $teamVal . "," . $team . "," . $teamName . "," . $playerId . "]";
		}
		if (strpos($lines[$i], "scoreInfo post centered") != "") {
			$start = strpos($lines[$i], "scoreInfo post centered") + 33;
			$score = substr($lines[$i], $start, strpos($lines[$i], "<", $start) - $start);
			
			if ($playerId == null) {
		    	//echo "<tr><td>ERROR: PLAYER NOT FOUND:" . $names[1] . " " . $names[0] . " (" . $team . ")</td></tr>";
		    	insertError($names[1], $names[0], $team, "PLAYERSCORES", $score); 
		    } else if ($score > 0 && !hasPlayerScoreAlreadyForRound($playerId, $round)) {
	           //echo "(" . $round . "," . $playerId . "," . $score . ")<br>";
			   addPlayerScore($round, $playerId, $score);
			   $countPlayers++;
	        }
		}
	}
	if ($countPlayers > 15 && $team != "") {
		$uploadedSomething = true;
		updateZeroScoringPlayers($round, $team);
	}
	
	return $uploadedSomething;
}

function parseSelectedTeams($textDump) {
   $players = getPlayerReplacementNames();
   $aflSiteRoundId = getAFLSiteRoundId();
   if (strpos($textDump, "roundid=" . $aflSiteRoundId . "&fixtureid=") != "") {
      $reqText = "starting" . substr($textDump, strpos($textDump, "<div class=\"gameTime\">"));
      $lines = preg_split("%\n%", $reqText);
      $count = count($lines);
      $round = getCurrentRound();
      $team = "";
      $team2 = "";
      $pos = "";
      $mapKey = "";
      $doneLoop3 = false;
      $index = 0;
      $posString = "";
      for ($i = 0; $i < $count; $i++) {
         if (strpos($lines[$i], "<div class=\"gameTime\">") != "") {
            for ($j = $i + 1; $j < $count; $j++) {
               if (strpos($lines[$j], "<h3>") != "") {      
                  $start = strpos($lines[$j], "<h3>") + 4;
                  $teams = preg_split("% v %", substr($lines[$j], $start, strpos($lines[$j], "<", $start) - $start));   
               }
               for ($k = $j + 1; $k < $count; $k++) {
                  if (strpos($lines[$k], "<div class=\"noData\">") != "") {
                     $i = $k + 1;
                     $doneLoop3 = true;
                     break;
                  }
                  else if (strpos($lines[$k], "<h4>") != "") {
                     $start = strpos($lines[$k], "<h4>") + 4;
                     $team = substr($lines[$k], $start, strpos($lines[$k], "<", $start) - $start);
                     $mapKey = getAFLTeamMapKey($team);
                     if (checkSelectedTeamAlreadySubmitted($round, $mapKey)) {
                     	break;
                     }
                     for ($m = $k + 1; $m < $count; $m++) {
                     	if (strpos($lines[$m], "<p class=\"pos\">") != "") {
                        	$start = strpos($lines[$m], "<strong>") + 8;
                           	$posString = substr($lines[$m], $start, strpos($lines[$m], ":", $start) - $start);
                           	if ($posString == "New") {
                           		break;
                           	}
                        	$index = 0;
                        }
                        else if (strpos($lines[$m], "target=\"_blank\"") != "") {
                        	$pos = getPosition($posString, $index);
							if ($pos == "IC" || $pos == "EMG") {
								$remText = $lines[$m];
								while (strpos($remText, "target=\"_blank\"") != "") {
									$start = strpos($remText, "target=\"_blank\"") + 16;
									processPlayer(substr($remText, $start, strpos($remText, "<", $start) - $start), $players, $mapKey, $pos, $team, $round);
									$remText = substr($remText, $start);
								}
							}
							else if ($posString != "In" && $posString != "Out") {
	                            $start = strpos($lines[$m], ">", strpos($lines[$m], "target=\"_blank\"")) + 1;
	                            $name = substr($lines[$m], $start, strpos($lines[$m], "<", $start) - $start);                   
	                            processPlayer($name, $players, $mapKey, $pos, $team, $round);
	                            $index++;
							}
                        }
                        else if (strpos($lines[$m], "<!-- end of team") != "") {
                           $k = $m;
                           break;
                        }
                     }
                  }
               }
               if ($doneLoop3) {
                  $doneLoop3 = false;
                  break;
               }
            }
         }
         else if (strpos($lines[$i], "END OF TEXT VIEW PAGE") != "") {
            break;
         }
      }
   }
}

function processPlayer($playerString, $players, $mapKey, $position, $team, $round) {
   $spaceIndex = strpos($playerString, " ");
   $firstName = trim(substr($playerString, 0, $spaceIndex));
   $surname = trim(substr($playerString, $spaceIndex + 1));
   $count2 = count($players);
   for ($k = 0; $k < $count2; $k++) {
      if ($players[$k]->firstName == $firstName && $players[$k]->surname == $surname && $players[$k]->team == getAFLTeam($mapKey)) {
         $firstName = $players[$k]->requiredFirstName;
	 $surname = $players[$k]->requiredSurname;
	    		
	 break;
      }
   }
	    	
   $playerId = getPlayerID($firstName, $surname, $mapKey);
   if ($playerId == null) {
   	  insertError($firstName, $surname, $team, "SELECTEDTEAMS", null);
      //echo "ERROR: PLAYER NOT FOUND:" . $firstName . " " . $surname . " (" . $team . ")<br>"; 
   }
   else {
      insertSelectTeamPlayer($playerId, $round, $position);
      //echo "INSERT INTO aflselectedteams (playerId, round, position) values ($playerId, $round, '$position');<br>";
   }
}

?>